//
//  SpecialDetailTableViewCell.m
//  Trip
//
//  Created by mingyang on 15-7-14.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SpecialDetailTableViewCell.h"

@implementation SpecialDetailTableViewCell

-(void)dealloc
{
    [_img release];
    [_label release];
    [super dealloc];
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.label = [[UILabel alloc]init];
        self.label.numberOfLines = 0;
        [self.contentView addSubview:self.label];
        [_label release];
        
        self.img = [[UIImageView alloc]init];
        [self.img.layer setCornerRadius:6.0];
        self.img.layer.masksToBounds = YES;
        [self.contentView addSubview:self.img];
        [_img release];
        
    }
    return self;
    
}


-(void)layoutSubviews
{
    [super layoutSubviews];
}


+ (CGFloat)heightWithText:(NSString *)text
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width - 10, 3000);
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}

+ (CGFloat)getImageViewHeight: (CGFloat)width andHeight:(CGFloat)height
{

    CGFloat h = [UIScreen mainScreen].bounds.size.width * height /width;
    return h;
}
- (void)setA:(ArticleSections *)a
{
    if (_a != a) {
        [_a release];
        _a = [a retain];
    }
    
    if ([a.specialDescription isEqual:[NSNull null]] == NO && [a.image_url isEqualToString:@""] == YES) {
        
        self.img.hidden = YES;
        self.label.hidden = NO;
        self.label.text = a.specialDescription;
        self.label.frame = CGRectMake(self.contentView.frame.origin.x + 5, self.contentView.frame.origin.y + 5, [UIScreen mainScreen].bounds.size.width - 10, [[self class]heightWithText:self.label.text]);
        
    }else if ([a.specialDescription isEqual:[NSNull null]] == YES && [a.image_url isEqualToString:@""] == NO){
        self.label.hidden = YES;
        self.img.hidden = NO;
        [self.img setImageWithURL:[NSURL URLWithString:a.image_url] placeholderImage:[UIImage imageNamed:@"place_holder.jpg"]];
        self.img.frame = CGRectMake(self.contentView.frame.origin.x + 5, self.contentView.frame.origin.y + 5, [UIScreen mainScreen].bounds.size.width - 10, [[self class] getImageViewHeight:a.image_width.floatValue andHeight:a.image_height.floatValue]);
    
    }else if ([a.specialDescription isEqual:[NSNull null]] == NO &&  [a.image_url isEqualToString:@""] == NO){
        self.img.hidden = NO;
        self.label.hidden = NO;
        [self.img setImageWithURL:[NSURL URLWithString:a.image_url] placeholderImage:[UIImage imageNamed:@"place_holder.jpg"]];
        self.label.text = a.specialDescription;
    self.img.frame = CGRectMake(self.contentView.frame.origin.x + 5, self.contentView.frame.origin.y + 5, [UIScreen mainScreen].bounds.size.width - 10, [[self class] getImageViewHeight:a.image_width.floatValue andHeight:a.image_height.floatValue]);
    

        self.label.frame = CGRectMake(self.img.frame.origin.x, self.img.frame.origin.y + self.img.frame.size.height + 2, self.img.frame.size.width, [[self class]heightWithText:self.label.text]);
        
    }
        
        
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
